const app = getApp();
import util from "../../utils/util";
import tools from "../../utils/tools";
Page({
    data: {
        showCot: false,
        folderList: [],
        id: "",
        index: "",
        ids: "",
        type: "",
    },

    onLoad: function (options) {
        // 监听页面加载的生命周期函数
        util.getUrl(app.globalData.url);
        this.setData({
            valueid: options.ids,
            value: decodeURIComponent(options.value),
            ids: options.ids,
            type: options.type,
            from_type: options.from_type,
        });

    },

    onShow() {
        this.getUserFilders();
    },

    // 获取用户全部收藏夹
    getUserFilders() {
        util.request("user.php", {
            action: "collect",
            option: "collect_list",
            user_id: ks.getStorageSync("user_id"),
            openid: ks.getStorageSync("openid"),
        }, "POST").then(res => {
            if (res.data.message == "success") {
                this.setData({
                    folderList: res.data.data,
                });
            }
        });
    },

    confirm() {
        this.data.type == "move" ? this.move() : this.addCollect(this.data.valueid, this.data.type_id);
    },

    chouseCollect(e) {
        let type_id = e.detail.value;
        this.setData({
            type_id,
        })
    },


    // 添加收藏接口
    addCollect(id, type_id) {
        let data = {
            action: "collect",
            option: "add",
            valueid: id,
            citycode: ks.getStorageSync("citycode"),
            user_id: ks.getStorageSync("user_id"),
            type_id,
            openid: ks.getStorageSync("openid"),
            path: "article",
            value: this.data.value,
        };
        util.request('user.php', data, 'POST').then(res => {
            if (res.data.state == 1) {
                ks.showToast({
                    title: res.data.message,
                });
                let pages = getCurrentPages(); //当前页面
                let prevPage = pages[pages.length - 2]; //上一页面
                prevPage.setData({ //直接给上移页面赋值
                    isCollect: true
                });
                ks.navigateBack({
                    delta: 1
                });
            }

        });
    },

    move() {
        if (ks.getStorageSync("openid")) {
            tools.moveCollect(this.data.type_id, this.data.valueid).then(res => ks.navigateBack());
        } else {
            util.getUrl(app.globalData.url).then((res) => {
                // 登录
                ks.login({
                    success: res => {
                        // 发送 res.code 到后台换取 openId, sessionKey, unionId
                        let loginData = {
                            action: "bdlogin",
                            // appid: accountInfo.appId,
                            appid: "15049213",
                            code: res.code,
                        };
                        util.request('user.php', loginData, 'POST').then(res => {
                            ks.setStorageSync("session_key", res.data.data.session_key);
                            ks.setStorageSync("openid", res.data.data.openid);
                            app.globalData.session_key = res.data.data.session_key;
                            tools.moveCollect(this.data.type_id, this.data.valueid).then(res => ks.navigateBack());
                        });
                    }
                });
            });
        }
    },

    onShareAppMessage(e) {}
});